CHARTS
Photo by Michael Fousert on Unsplash
Behind the clouds, the sky is always blue…
— Norwegian Proverb
df = read_csv("archetypes/energy-consumption-by-country/energy.csv")
df
This operation will simplify the data to three energy type groups: Renewable, Nuclear, and Conventional Thermal.
df_final <- df %>%
mutate(
type = fct_other(
type,
keep = c("Nuclear", "Conventional thermal"),
other_level="Renewable"
),
type = fct_relevel(type, "Renewable", "Nuclear", "Conventional thermal")
) %>%
select(country, country_name, type, value=`2018`) %>%
group_by(country, country_name, type) %>%
summarize(value = sum(value)) %>%
ungroup()
df_final
df_country_sort <- df_final %>%
mutate(type = fct_other(type,
keep = c("Conventional thermal"),
other_level="non_co2_pollutant")) %>%
group_by(country, type) %>%
summarise(upper_value = sum(value)) %>%
ungroup() %>%
group_by(country) %>%
mutate(total = sum(upper_value)) %>%
ungroup() %>%
filter(!type == "Conventional thermal") %>%
mutate(percent_clean = 100 * upper_value / total) %>%
arrange(desc(percent_clean)) %>%
select(country, percent_clean)
df_country_sort
df_country_production <- df_final %>%
group_by(country) %>%
summarise(prod_by_country = sum(value)) %>%
ungroup() %>%
group_by() %>%
mutate(prod_europe = sum(prod_by_country)) %>%
ungroup() %>%
group_by(country) %>%
summarize(percent_production = 100 * prod_by_country / prod_europe) %>%
ungroup() %>%
arrange(desc(percent_production))
df_country_production
df_production_by_type <- df_final %>%
pivot_wider(names_from = type, values_from = value)
df_production_by_type
We’ll now have all the needed measure for each energy type and the percent of production vs percent clean.
df_join <- inner_join(df_production_by_type, df_country_production) %>%
inner_join(df_country_sort) %>%
arrange(desc(percent_clean)) %>%
mutate(xmax=cumsum(percent_production),
xmin=xmax-percent_production) %>%
mutate(total_country_energy = Renewable + Nuclear + `Conventional thermal`,
percent_renewable = 100 * Renewable / total_country_energy,
percent_nuclear = 100 * Nuclear / total_country_energy,
percent_thermal = 100 * `Conventional thermal` / total_country_energy)
df_join
theme_opts <- theme(
text = element_text(family = "inconsolata", size = 16),
plot.title = element_text(color = "black", size = 16, face = "bold"),
plot.subtitle = element_text(color = "black", size = 12),
plot.caption = element_text(color = "#555555", size = 11),
panel.border = element_blank(),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
legend.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
legend.position = "top"
)
fill_colors <- c("Renewable" = "#FF9800",
"Nuclear" = "#F8EE5D",
"Conventional thermal" = "#D2D2D2")
v1 <- ggplot(df_join) +
geom_rect(aes(ymin = 0, ymax = percent_nuclear, xmin = xmin, xmax = xmax,
fill = "Nuclear"),
color = "#FFFFFF") + # F6EC3A
geom_rect(aes(ymin=percent_nuclear, ymax=percent_nuclear + percent_renewable,
xmin = xmin, xmax = xmax,
fill = "Renewable"),
color = "#FFFFFF") + # EABB26
geom_rect(aes(ymin = 0, ymax = -percent_thermal,
xmin = xmin, xmax = xmax,
fill = "Conventional thermal"),
color = "#FFFFFF") + # C4C4C4
scale_fill_manual(values = fill_colors) +
geom_text(aes(x = (xmin+xmax)/2,
y = percent_nuclear + percent_renewable,
label = country),
size = 3,
vjust = -0.5, hjust = 0.5, family = "inconsolata") +
labs(title = "How European countries generated electricity in 2018") +
theme_bw() +
theme_opts
girafe(ggobj = v1, width_svg = 16, height_svg = 9,
options = list(opts_sizing(rescale = TRUE, width = 0.8)))
offset <- 2
v2 <- v1 +
geom_segment(aes(x=-4, xend=106, y=100, yend=100), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=75, yend=75), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=50, yend=50), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=25, yend=25), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=0, yend=0), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=-25, yend=-25), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=-50, yend=-50), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=-75, yend=-75), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
geom_segment(aes(x=-4, xend=106, y=-100, yend=-100), colour = "#EEEEEE", size = 0.5, linetype="dotted") +
annotate("text", x = -4, y = (0 + offset), label = "0%", vjust = 0, hjust = 0, family = "inconsolata") +
annotate("text", x = -4, y = (-25 + offset), label = "25%", vjust = 0,hjust = 0, family = "inconsolata") +
annotate("text", x = -4, y = (-50 + offset), label = "50%", vjust = 0,hjust = 0, family = "inconsolata") +
annotate("text", x = -4, y = (-75 + offset), label = "75%", vjust = 0,hjust = 0, family = "inconsolata") +
annotate("text", x = -4, y = (-100 + offset), label = "100% conventional thermal energy", vjust = 0, hjust = 0, family = "inconsolata") +
annotate("text", x = 106, y = (0 + offset), label = "0%", vjust = 0, hjust = 1, family = "inconsolata") +
annotate("text", x = 106, y = (25 + offset), label = "25%", vjust = 0, hjust = 1, family = "inconsolata") +
annotate("text", x = 106, y = (50 + offset), label = "50%", vjust = 0, hjust = 1, family = "inconsolata") +
annotate("text", x = 106, y = (75 + offset), label = "75%", vjust = 0, hjust = 1, family = "inconsolata") +
annotate("text", x = 106, y = (100 + offset), label = "clean energy 100%", vjust = 0, hjust = 1, family = "inconsolata") +
annotate("text", x = 1, y = 118,
label="Norway had an electricity production almost entirely made up of renewable\nenergy (97%). It is the second largest producer of this type of energy in Europe.\n \u2193",
vjust = 1.0, hjust = 0, family = "inconsolata") +
annotate("text", x = 50, y = 65,
label="Germany is the larger energy producer in\nEurope and also produce the most renewable\nand conventional thermal energy (representing\n31% and 56% of production, respectively.)\n \u2193",
hjust = 0, family = "inconsolata") +
annotate("text", x = 0, y = -13,
label="France is the second largest \u2191\nenergy producer in Europe\nbut by far the largest nuclear energy\nprovider representing 71% of its production.",
vjust = 1.0, hjust = 0, family = "inconsolata") +
annotate("text", x = 71, y = -85,
label="Most of Poland's energy production is \u2192\nfrom conventional thermal energy (90%).",
vjust = 1.0, hjust = 0, family = "inconsolata")
girafe(ggobj = v2, width_svg = 16, height_svg = 9,
options = list(opts_sizing(rescale = TRUE, width = 0.8)))